package com.tomcov.client.activities;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.tomcov.client.ClientFactory;
import com.tomcov.client.i18n.ErrorMessageConstants;
import com.tomcov.client.place.MapPlace;
import com.tomcov.client.place.WelcomPlace;
import com.tomcov.client.service.UserServiceAsync;
import com.tomcov.client.view.SettingsView;
import com.tomcov.shared.UserInfo;

public class SettingsActivity extends AbstractActivity implements SettingsView.Presenter{

	private ClientFactory clientFactory;


	/**
	 * Instantiates a new settings activity.
	 *
	 * @param clientFactory the client factory
	 */
	public SettingsActivity(ClientFactory clientFactory){
		this.clientFactory = clientFactory;	
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		SettingsView SettingsView = clientFactory.getSettingsView();
		SettingsView.getConfirmChange().addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				SettingsView SettingsView = clientFactory.getSettingsView();
				SettingsView.getConfirmChange().setEnabled(false);
				SettingsView.getConfirmChange().setVisible(false);
				UpdateClient();
				SettingsView.getConfirmChange().setVisible(true);
				SettingsView.getConfirmChange().setEnabled(true);
			}	
		});
		SettingsView.setPresenter(this);
		panel.setWidget(SettingsView.asWidget());


	}

	@Override
	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);		
	}

	public void UpdateClient(){
		SettingsView SettingsView = clientFactory.getSettingsView();
		final ErrorMessageConstants emvc = GWT.create(ErrorMessageConstants.class);
		
		if((SettingsView.getEmailAdress().getValue()==null || SettingsView.getPassword().getValue()==null ||
				SettingsView.getFirstName().getValue()==null || SettingsView.getLastName().getValue()==null|| 
				SettingsView.getAge()==null||SettingsView.getConsommation().getValue()==null||SettingsView.getMark().
				getValue()==null||SettingsView.getModel().getValue()==null || SettingsView.getPhone().getValue()==null)
				==true){
			Window.alert(emvc.fillallthefield());
		}
		else if(SettingsView.getEmailAdress().getValue().matches("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$")==false){
			Window.alert(emvc.invalidemail());
		}
		else if(SettingsView.getPhone().getValue().matches("^[0-9]{9,}$")==false){
			Window.alert(emvc.invalidphone());
		}
		else if(SettingsView.getConsommation().getValue().matches("^[0-9]{1,2}+\\.[0-9]{1,2}$||^[0-9]{1,2}$")==false){
			Window.alert(emvc.invalidconsommation());
		}
		else{
			
			UserServiceAsync USA = clientFactory.getUSA();
			USA.getUserInSession(new AsyncCallback<UserInfo>(){
				@Override
				public void onFailure(Throwable caught) {
					Window.alert(emvc.serverfail());
				}
				@Override
				public void onSuccess(UserInfo result) {
					if(result==null){Window.alert("fail getuserinsession"); 
					goTo(new WelcomPlace(null));}
					else{
						UserServiceAsync USA = clientFactory.getUSA();
						UserInfo usermodify = getmodify(result);
						USA.updateUserInfo(result, usermodify, new AsyncCallback<UserInfo>(){
							@Override
							public void onFailure(Throwable caught) {
								Window.alert("server fail to modify your data");
							}
							@Override
							public void onSuccess(UserInfo usermodifyok) {
								if(usermodifyok==null){
									Window.alert("data not modify");}
								else{
									//Window.alert("data modify");
									goTo(new MapPlace(null));
								}
							}
						});
					}
				}
			});
		}
	}

	private UserInfo getmodify(UserInfo info){
		SettingsView SettingsView = clientFactory.getSettingsView();

		UserInfo usermodify = info;/*new UserInfo(info.getUserName(),SettingsView.getEmailAdress().getValue(),SettingsView.getPassword().
				getValue(),SettingsView.getFirstName().getValue(),SettingsView.getLastName().getValue(),
				SettingsView.getLangage());*/
		
		
		usermodify.setEmailAddress(SettingsView.getEmailAdress().getValue());
		usermodify.setPassword(SettingsView.getPassword().getValue());
		usermodify.setFirstName(SettingsView.getFirstName().getValue());
		usermodify.setLastName(SettingsView.getLastName().getValue());
		usermodify.setLangage(SettingsView.getLangage());
		usermodify.setAge(SettingsView.getAge().getValue());
		usermodify.setCons(SettingsView.getConsommation().getValue());
		usermodify.setMark(SettingsView.getMark().getValue());
		usermodify.setModel(SettingsView.getModel().getValue());
		usermodify.setPlace(SettingsView.getplace());
		usermodify.setPhone(SettingsView.getPhone().getValue());

		return usermodify;
	}

}

